package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCommentImage;
import cn.wolfcode.trip.base.mapper.StrategyCommentImageMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 攻略点评的实现类
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategyCommentImageMapper commentImageMapper;

    @Override
    public PageInfo query(StrategyCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public StrategyComment getCommentContentById(Long strategyCommentId) {
        List<StrategyCommentImage> commentImages = commentImageMapper.selectCommentImageByCommentId(strategyCommentId);
        StrategyComment strategyComment = strategyCommentMapper.selectByPrimaryKey(strategyCommentId);
        strategyComment.setCommentImages(commentImages);
        return strategyComment;
    }

    @Override
    public void changeStrategyCommentState(Long id, Integer state) {
        // 判断是发布 / 拒绝类型的操作
        if (state == 2){
            // 审核通过,设置时间
            strategyCommentMapper.changeStrategyCommentState(id,state,new Date());
        } else {
            // 拒绝操作
            strategyCommentMapper.changeStrategyCommentState(id,state,null);
        }

    }

    @Override
    public void changeStrategyCommentCommend(Long id, Integer isCommned) {
        if(isCommned == 0){
            isCommned = 1;
        }else {
            isCommned = 0;
        }
        strategyCommentMapper.changeStrategyCommentCommend(id,isCommned);
    }

    @Override
    public void saveOrUpdate(StrategyComment entity, String[] images) {
        if(entity.getId() == null) {
            //设置点评时间
            entity.setCreateTime(new Date());
            //保存点评对象
            strategyCommentMapper.insert(entity);

            //保存图片
            if(images.length != 0) {
                for (String image : images) {
                    StrategyCommentImage commentImage = new StrategyCommentImage();
                    commentImage.setStrategyCommentId(entity.getId());//点评对象保存后的id
                    commentImage.setImage(image);
                    commentImageMapper.insert(commentImage);
                }
            }
        }else {
            //修改操作,暂没需求
        }
    }

    @Override
    public void updateGreatCount(Integer greats, Long strategyCommentId) {
        strategyCommentMapper.updateGreatCount(greats,strategyCommentId);
    }

    @Override
    public StrategyComment get(Long id) {
        StrategyComment strategyComment = strategyCommentMapper.selectByPrimaryKey(id);
        return strategyComment;
    }

    @Override
    public PageInfo getStrategyCommentsByAuthorId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List list = strategyCommentMapper.getStrategyCommentsByAuthorId(qo);
        return new PageInfo(list);
    }

    @Override
    public Integer getCountByAuthorId(QueryObject qo) {
        Integer count = strategyCommentMapper.getCountByAuthorId(qo);
        return count;
    }
}
